package com.atguigu.gulimall.order.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyMQConfig {
    /**
     出现问题： 并没创建交换机、队列、绑定关系

     出现问题的原因：只有当第一次连接上RabbitMQ时，发现没有这些东西才会创建

     解决方案：监听任一队列。创建后注释掉
     * @param message
     */
//    @RabbitListener(queues = "order.release.queue")
//    public void handle(Message message){
//
//    }

    @Bean
    public Exchange orderEventExchange(){
        return new TopicExchange("order-event-exchange", true, false);
    }

    @Bean
    public Queue orderDelayQueue(){
        Map<String,Object> args=new HashMap<>();
        args.put("x-dead-letter-exchange","order-event-exchange");
        args.put("x-dead-letter-routing-key","order.release.order");
        args.put("x-message-ttl",60000);
        Queue queue = new Queue("order.delay.queue", true, false, false,args);
        return queue;
    }

    @Bean
    public Queue orderReleaseQueue(){
        return new Queue("order.release.queue", true, false, false);
    }

    @Bean
    public Queue orderSeckillQueue(){
        return new Queue("order.seckill.queue", true, false, false);
    }

    @Bean
    public Binding orderCreateOrderBinding(){
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }

    @Bean
    public Binding orderReleaseOrderBinding(){
        return new Binding("order.release.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

    /**
     * 订单交换机直接绑定库存解锁队列
     * @return
     */
    @Bean
    public Binding orderReleaseOthterBinding(){
        return new Binding("stock.release.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other",
                null);
    }

    /**
     * 订单交换机直接绑定库存解锁队列
     * @return
     */
    @Bean
    public Binding orderSeckillBinding(){
        return new Binding("order.seckill.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.order",
                null);
    }
}
